"use strict";
const Controller = require("egg").Controller;

class GiftController extends Controller {
  //列表
  async index() {
    const { ctx, app } = this;

    let data = await ctx.page("Gift");
    await ctx.renderTemplate({
      title: "礼物管理",
      tempType: "table",
      table: {
        // 按钮
        buttons: {
          //新增
          add: "/admin/gift/create",
        },
        //表头
        columns: [
          {
            title: "礼物名",
            fixed: "left",
            render(item) {
              return `
              <h2 class="table-avatar">
                  <a class="avatar avatar-sm mr-2"><img class="avatar-img rounded-circle bg-light" src="${item.image}"></a>
              </h2>`;
            },
          },
          {
            title: "金币",
            key: "coin",
            width: 180,
            fixed: "center",
          },
          {
            title: "创建时间",
            key: "created_time",
            width: 180,
            fixed: "center",
          },
          {
            title: "操作",
            width: 200,
            fixed: "center",
            action: {
              edit: function (id) {
                return `/admin/gift/edit/${id}`;
              },
              delete: function (id) {
                return `/admin/gift/delete/${id}`;
              },
            },
          },
        ],
      },
      data,
    });
  }
  //创建礼物表单
  async create() {
    const { ctx } = this;
    await ctx.renderTemplate({
      // 页面标题
      title: "创建礼物",
      // 模板类型 form表单，table表格分页
      tempType: "form",
      // 表单配置
      form: {
        // 提交地址
        action: "/admin/gift",
        // 字段配置
        fields: [
          {
            label: "礼物名称",
            type: "text",
            name: "name",
            placeholder: "礼物名称",
          },
          {
            label: "礼物图标",
            type: "file",
            name: "image",
          },
          {
            label: "金币",
            type: "number",
            name: "coin",
            default: 0,
          },
        ],
      },
      // 新增成功跳转路径
      successUrl: "/admin/gift",
    });
  }
  //创建礼物逻辑
  async save() {
    const { ctx, app } = this;
    ctx.validate({
      name: {
        type: "string",
        required: true,
        desc: "礼物名称",
      },
      image: {
        type: "string",
      },
      coin: {
        type: "int",
      },
    });
    let { name, image, coin } = ctx.request.body;

    // 创建礼物
    let gift = await app.model.Gift.create({
      name,
      image,
      coin,
    });
    if (!gift) {
      ctx.throw(400, "创建礼物失败");
    }
    ctx.apiSuccess(gift);
  }
  //编辑表单
  async edit() {
    const { ctx, app } = this;
    const id = ctx.params.id;

    let data = await app.model.Gift.findOne({
      where: {
        id,
      },
    });

    if (!data) {
      return await ctx.pageFail("该记录不存在");
    }

    data = JSON.parse(JSON.stringify(data));

    await ctx.renderTemplate({
      id: ctx.params.id,
      title: "修改礼物",
      tempType: "form",
      form: {
        // 提交地址
        action: "/admin/gift/" + ctx.params.id,
        fields: [
          {
            label: "礼物名称",
            type: "text",
            name: "name",
            placeholder: "礼物名称",
          },
          {
            label: "礼物图标",
            type: "file",
            name: "image",
          },
          {
            label: "金币",
            type: "number",
            name: "coin",
            default: 0,
          },
        ],
        data,
      },
      // 新增成功跳转路径
      successUrl: "/admin/gift",
    });
  }
  //更新逻辑
  async update() {
    const { ctx, app } = this;
    ctx.validate({
      id: {
        type: "int",
        required: true,
      },
      name: {
        type: "string",
        required: true,
      },
      image: {
        type: "string",
      },
      coin: {
        type: "int",
      },
    });

    let id = ctx.params.id;
    let { name,image,coin  } = ctx.request.body;
    let gift = await app.model.Gift.findOne({
      where: {
        id,
      },
    });
    if (!gift) {
      return ctx.apiFail("礼物不存在");
    }
  
    gift.name = name;
    gift.image = image;
    gift.coin = coin;
    ctx.apiSuccess(await gift.save());
  }
   //删除礼物
   async delete() {
    const { ctx, app } = this;
    const id = ctx.params.id;
    await app.model.Gift.destroy({
      where: {
        id,
      },
    });

    ctx.toast("删除成功", "success");
    ctx.redirect("/admin/gift");
  }
}

module.exports = GiftController;
